package com.hjs.servlet.regularinspection.pavementdamage;

import com.hjs.mapper.RegularInspectionMapper;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/PavementDamage/delete")
@Slf4j
public class DeletePavementDamage extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        RequestUtil requestUtil = new RequestUtil(request, response);
        /*
         * 需提供删除类型 deleteType
         */
        switch (Integer.parseInt(request.getParameter("deleteType"))){
            case 1:
                /*
                 * 提供参数
                 * pavementDamageComID
                 */
                deletePavementDamageCom(requestUtil);
                break;
            case 2:
                /*
                 * 提供参数
                 * pavementDamageID
                 */
                deletePavementDamage(requestUtil);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + Integer.parseInt(request.getParameter("deleteType")));
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        doPost(request,response);
    }

    private void deletePavementDamageCom(RequestUtil requestUtil) {
        try (SqlSession sqlSession = Mapper.getSqlSession()) {
            RegularInspectionMapper mapper = sqlSession.getMapper(RegularInspectionMapper.class);
            boolean sqlResult = mapper.deleteOnePavementDamageComById(requestUtil.getInt("pavementDamageComID"));
            Mapper.SessionEnd(sqlResult, sqlSession,"删除路面损坏情况失败");
            requestUtil.responseIsSuccess(sqlResult,"删除失败");
        } catch (Exception e) {
            log.error(e.toString());
            requestUtil.responseFail("未知错误");
        }
    }

    private void deletePavementDamage(RequestUtil requestUtil) {
        try (SqlSession sqlSession = Mapper.getSqlSession()) {
            RegularInspectionMapper mapper = sqlSession.getMapper(RegularInspectionMapper.class);
            boolean sqlResult = mapper.deleteOnePavementDamageComById(requestUtil.getInt("pavementDamageID"));
            Mapper.SessionEnd(sqlResult, sqlSession,"删除路面损坏情况细则失败");
            requestUtil.responseIsSuccess(sqlResult,"删除失败");
        } catch (Exception e) {
            log.error(e.toString());
            requestUtil.responseFail("未知错误");
        }
    }
}
